import { onMounted, onUnmounted } from 'vue'

/**
 * 键盘快捷键管理组合式函数
 */
export interface KeyboardShortcut {
  key: string
  ctrl?: boolean
  shift?: boolean
  alt?: boolean
  handler: () => void
  description: string
}

export function useKeyboardShortcuts(shortcuts: KeyboardShortcut[]) {
  const handleKeyDown = (event: KeyboardEvent) => {
    // 如果焦点在输入框中，不处理快捷键
    const target = event.target as HTMLElement
    if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.contentEditable === 'true') {
      return
    }

    for (const shortcut of shortcuts) {
      const keyMatch = event.key.toLowerCase() === shortcut.key.toLowerCase()
      const ctrlMatch = !!shortcut.ctrl === event.ctrlKey
      const shiftMatch = !!shortcut.shift === event.shiftKey
      const altMatch = !!shortcut.alt === event.altKey

      if (keyMatch && ctrlMatch && shiftMatch && altMatch) {
        event.preventDefault()
        event.stopPropagation()
        shortcut.handler()
        break
      }
    }
  }

  onMounted(() => {
    document.addEventListener('keydown', handleKeyDown)
  })

  onUnmounted(() => {
    document.removeEventListener('keydown', handleKeyDown)
  })

  return {
    shortcuts
  }
}

/**
 * 文件管理快捷键预设
 */
export function useFileManagerShortcuts(handlers: {
  onUpload?: () => void
  onCreateFolder?: () => void
  onRefresh?: () => void
  onSelectAll?: () => void
  onDelete?: () => void
  onCopy?: () => void
  onPaste?: () => void
  onRename?: () => void
  onPreview?: () => void
  onDownload?: () => void
  onSearch?: () => void
  onToggleView?: () => void
  onToggleSidebar?: () => void
  onEscape?: () => void
}) {
  const shortcuts: KeyboardShortcut[] = [
    // 文件操作
    {
      key: 'u',
      ctrl: true,
      handler: handlers.onUpload || (() => {}),
      description: 'Ctrl+U: 上传文件'
    },
    {
      key: 'n',
      ctrl: true,
      shift: true,
      handler: handlers.onCreateFolder || (() => {}),
      description: 'Ctrl+Shift+N: 新建文件夹'
    },
    {
      key: 'F5',
      handler: handlers.onRefresh || (() => {}),
      description: 'F5: 刷新'
    },
    {
      key: 'r',
      ctrl: true,
      handler: handlers.onRefresh || (() => {}),
      description: 'Ctrl+R: 刷新'
    },
    
    // 选择操作
    {
      key: 'a',
      ctrl: true,
      handler: handlers.onSelectAll || (() => {}),
      description: 'Ctrl+A: 全选'
    },
    
    // 编辑操作
    {
      key: 'Delete',
      handler: handlers.onDelete || (() => {}),
      description: 'Delete: 删除选中项'
    },
    {
      key: 'c',
      ctrl: true,
      handler: handlers.onCopy || (() => {}),
      description: 'Ctrl+C: 复制'
    },
    {
      key: 'v',
      ctrl: true,
      handler: handlers.onPaste || (() => {}),
      description: 'Ctrl+V: 粘贴'
    },
    {
      key: 'F2',
      handler: handlers.onRename || (() => {}),
      description: 'F2: 重命名'
    },
    
    // 预览和下载
    {
      key: 'Enter',
      handler: handlers.onPreview || (() => {}),
      description: 'Enter: 预览选中文件'
    },
    {
      key: ' ',
      handler: handlers.onPreview || (() => {}),
      description: 'Space: 快速预览'
    },
    {
      key: 'd',
      ctrl: true,
      handler: handlers.onDownload || (() => {}),
      description: 'Ctrl+D: 下载选中文件'
    },
    
    // 搜索和导航
    {
      key: 'f',
      ctrl: true,
      handler: handlers.onSearch || (() => {}),
      description: 'Ctrl+F: 搜索文件'
    },
    {
      key: 'Tab',
      ctrl: true,
      handler: handlers.onToggleView || (() => {}),
      description: 'Ctrl+Tab: 切换视图模式'
    },
    {
      key: 'b',
      ctrl: true,
      handler: handlers.onToggleSidebar || (() => {}),
      description: 'Ctrl+B: 切换侧边栏'
    },
    
    // 通用操作
    {
      key: 'Escape',
      handler: handlers.onEscape || (() => {}),
      description: 'Esc: 取消当前操作'
    }
  ]

  return useKeyboardShortcuts(shortcuts)
}

/**
 * 预览窗口快捷键预设
 */
export function usePreviewShortcuts(handlers: {
  onClose?: () => void
  onFullscreen?: () => void
  onDownload?: () => void
  onNext?: () => void
  onPrevious?: () => void
  onZoomIn?: () => void
  onZoomOut?: () => void
  onRotate?: () => void
}) {
  const shortcuts: KeyboardShortcut[] = [
    // 窗口控制
    {
      key: 'Escape',
      handler: handlers.onClose || (() => {}),
      description: 'Esc: 关闭预览'
    },
    {
      key: 'F11',
      handler: handlers.onFullscreen || (() => {}),
      description: 'F11: 全屏切换'
    },
    {
      key: 'f',
      handler: handlers.onFullscreen || (() => {}),
      description: 'F: 全屏切换'
    },
    
    // 文件操作
    {
      key: 'd',
      ctrl: true,
      handler: handlers.onDownload || (() => {}),
      description: 'Ctrl+D: 下载文件'
    },
    
    // 导航
    {
      key: 'ArrowRight',
      handler: handlers.onNext || (() => {}),
      description: '→: 下一个文件'
    },
    {
      key: 'ArrowLeft',
      handler: handlers.onPrevious || (() => {}),
      description: '←: 上一个文件'
    },
    
    // 图片/视频控制
    {
      key: '=',
      ctrl: true,
      handler: handlers.onZoomIn || (() => {}),
      description: 'Ctrl+=: 放大'
    },
    {
      key: '-',
      ctrl: true,
      handler: handlers.onZoomOut || (() => {}),
      description: 'Ctrl+-: 缩小'
    },
    {
      key: 'r',
      handler: handlers.onRotate || (() => {}),
      description: 'R: 旋转图片'
    },
    
    // 视频控制
    {
      key: ' ',
      handler: () => {
        // 播放/暂停视频
        const video = document.querySelector('video') as HTMLVideoElement
        if (video) {
          if (video.paused) {
            video.play()
          } else {
            video.pause()
          }
        }
      },
      description: 'Space: 播放/暂停视频'
    },
    {
      key: 'ArrowUp',
      handler: () => {
        // 增加音量
        const video = document.querySelector('video') as HTMLVideoElement
        if (video) {
          video.volume = Math.min(1, video.volume + 0.1)
        }
      },
      description: '↑: 增加音量'
    },
    {
      key: 'ArrowDown',
      handler: () => {
        // 减少音量
        const video = document.querySelector('video') as HTMLVideoElement
        if (video) {
          video.volume = Math.max(0, video.volume - 0.1)
        }
      },
      description: '↓: 减少音量'
    }
  ]

  return useKeyboardShortcuts(shortcuts)
}
